package com.controller;

import com.pojo.*;
import com.server.impl.ProductServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description：产品管理
 */
@Controller
@RequestMapping("product")
public class ProductController {
    @Resource
    ProductServiceImpl productService;

    //添加界面
    @RequestMapping("/getpicture")
    public String picture(){return "pages/picture-add";}

    @RequestMapping(value ="/sort",method = RequestMethod.GET)
    public String sort(Model model){
        List<Product> productList= productService.getproductList();
        model.addAttribute("productList",productList);

        return "/pages/Products_List" ;
    }

    /*模糊查找*/
    /*@RequestParam(value="product_id")int product_id, HttpServletRequest request, Map<String,Object> map, @RequestParam(value = "currentPage",defaultValue = "1",required = false)int currentPage, Model model, HttpSession session*/
    @RequestMapping("/findproduct")
    @ResponseBody
    public ModelAndView findproduct(String product_name) {
        //1.调用service方法
        List<Product> productList = productService.findproduct(product_name);
        //2.实例化一个ModelAndView对象
        ModelAndView mv=new ModelAndView();
        mv.addObject("productList",productList);
        mv.setViewName("/pages/Products_List");
        return mv;

    }

    @RequestMapping(value ="/edit1",method = RequestMethod.GET)
    @ResponseBody
    public Result updateproduct(@Param("product_id") Integer product_id, Model model, HttpSession session){
        Result result = new Result();
        if (session.getAttribute("product1")!=null){
            session.removeAttribute("product1");
        }

        Product product1=productService.selectProductById(product_id);
        System.out.println(product1);

        session.setAttribute("product1",product1);

        if (product1!=null){
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);

        return result;

    }
    @RequestMapping(value = "/addproduct",method = RequestMethod.GET)
    @ResponseBody
    public Result addproduct(@Param("product_id") int product_id,@Param("product_name") String product_name,
      @Param("product_s_price")String product_s_price, @Param("product_price")String product_price,Model model, HttpSession session){
        Result result = new Result();
        //当前时间日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        Date d = new Date(System.currentTimeMillis());
        int count1= productService.addproduct(product_id, product_name, product_s_price, product_price,d);

        if (count1>0){
            List<Product> productList=productService.getproductList();
            //model.addAttribute("productList",productList);
            session.setAttribute("productList",productList);
            result.setSuccess(true);
        }else {
            result.setMsg("添加失败");
        }
        return result;
    }

    @RequestMapping(value ="/updateproduct",method = RequestMethod.GET)
    @ResponseBody
    public Result updateproduct(@Param("product_id") int product_id,@Param("product_name")String product_name,
         @Param("product_s_price")String product_s_price,@Param("product_price")String product_price,HttpSession session){
        /*int member_id,String member_name,String member_nickname,String member_gender,String member_phone,String member_email*/
        Result result = new Result();

        int count=productService.updateproduct(product_id, product_name, product_s_price, product_price);
        session.removeAttribute("product1");
        if (count>0){
            System.out.println("修改商品成功");
            result.setSuccess(true);
            return result;
        }
        System.out.println("修改商品失败");
        result.setSuccess(false);

        return result;
    }

    @RequestMapping(value ="/delproduct",method = RequestMethod.GET)
    public String delproduct(int product_id,Model model){
        productService.delproduct(product_id);
        return "pages/Products_List";
    }
    @RequestMapping("/addByID")
    public String addByID(int product_id)
    {
        /*int result = adminService.deleteById(Integer.valueOf(adminid));*/
        productService.addByID(product_id);

        return "redirect:pages/Products_List";
    }


}
